
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       battery_monitor_analog.h
  * @author     baiyang
  * @date       2022-10-27
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include "battery_monitor_backend.h"

#include <analogin_manager/analogin.h>
/*-----------------------------------macro------------------------------------*/
// default pins and dividers
#if defined(HAL_BATT_VOLT_PIN)
 // pins defined in board config (hwdef.dat on ChibiOS)
 # define MB_BATT_VOLT_PIN                  HAL_BATT_VOLT_PIN
 # define MB_BATT_CURR_PIN                  HAL_BATT_CURR_PIN
 # define MB_BATT_VOLTDIVIDER_DEFAULT       (HAL_BATT_VOLT_SCALE * 0.01f)
 # define MB_BATT_CURR_AMP_PERVOLT_DEFAULT  (HAL_BATT_CURR_SCALE * 0.01f)
#elif CONFIG_HAL_BOARD == HAL_BOARD_RTTHREAD
 # define MB_BATT_VOLT_PIN                  4
 # define MB_BATT_CURR_PIN                  3
 # define MB_BATT_VOLTDIVIDER_DEFAULT       10.1f
 # define MB_BATT_CURR_AMP_PERVOLT_DEFAULT  17.0f
#else
 # define MB_BATT_VOLT_PIN                  -1
 # define MB_BATT_CURR_PIN                  -1
 # define MB_BATT_VOLTDIVIDER_DEFAULT       10.1f
 # define MB_BATT_CURR_AMP_PERVOLT_DEFAULT  17.0f
#endif

// This is 0 for the majority of the power modules.
#ifndef MB_BATT_CURR_AMP_OFFSET_DEFAULT
 #define MB_BATT_CURR_AMP_OFFSET_DEFAULT 0.0f
#endif

// Other values normally set directly by mission planner
// # define MB_BATT_VOLTDIVIDER_DEFAULT 15.70   // Volt divider for AttoPilot 50V/90A sensor
// # define MB_BATT_VOLTDIVIDER_DEFAULT 4.127   // Volt divider for AttoPilot 13.6V/45A sensor
// # define MB_BATT_CURR_AMP_PERVOLT_DEFAULT 27.32  // Amp/Volt for AttoPilot 50V/90A sensor
// # define MB_BATT_CURR_AMP_PERVOLT_DEFAULT 13.66  // Amp/Volt for AttoPilot 13.6V/45A sensor
/*----------------------------------typedef-----------------------------------*/
typedef struct battery_monitor_analog* battery_monitor_analog_t;

struct battery_monitor_analog {
    struct battery_monitor_backend      backend;

    analog_source_backend_t             _volt_pin_analog_source;
    analog_source_backend_t             _curr_pin_analog_source;
};

/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void battery_monitor_analog_ctor(battery_monitor_analog_t battmonitor_analog, battery_monitor_state_t mon_state, battery_monitor_params_t params);
battery_monitor_backend_t battery_monitor_analog_probe(battery_monitor_state_t mon_state, battery_monitor_params_t params);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



